//package cn.itcast.service;
//
//import cn.itcast.dao.dm.DMCodeTargetTypeMapper;
//import cn.itcast.model.TCodeTargetType;
//import cn.itcast.model.TargetTree;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
//@Service
//public class targetService {
//    @Autowired
//    private DMCodeTargetTypeMapper mapper;
//    public List<TCodeTargetType> findAll() {
//        List<TCodeTargetType> list = mapper.selectAll();
//        return list;
//    }
//    public void insert(TCodeTargetType targetType) {
//        targetType.setCreateDate(new Date());
////        targetType.setIs_inherent(0);
//        mapper.insertSelective(targetType);
//        System.out.println(targetType.getId());
//    }
//    public void update(TCodeTargetType targetType) {
//        targetType.setUpdateDate(new Date());
//        mapper.updateByPrimaryKeySelective(targetType);
//    }
//    public String delete(Integer primaryKey) {
//        Integer count=mapper.selectByPid(primaryKey);
//        if (count>0){
//          return "请先删除子级数据";
//        }
//        mapper.deleteByPrimaryKey(primaryKey);
//        return "删除成功";
//    }
//    public TCodeTargetType selectByPrimaryKey(Integer primaryKey){
//        return mapper.selectByPrimaryKey(primaryKey);
//    }
//
//    public List<TCodeTargetType> likeQuery(String targetName) {
//        return mapper.likeQuery(targetName);
//    }
//
//    public List<TargetTree> findAllByTree() {
//        List<TargetTree> list = mapper.getTargetTree();
//        /*
//        将所有数据按照parentId不同从list放到不同的map集合里
//         */
//        HashMap<String, List<TargetTree>> map = new HashMap<>();
//        for (TargetTree targetTree : list) {
//            Integer parentId = targetTree.getParentId();
//            if (map.containsKey(parentId.toString())){
//                map.get(parentId.toString()).add(targetTree);
//            }else {
//                ArrayList<TargetTree> subList = new ArrayList<>();
//                subList.add(targetTree);
//                map.put(parentId.toString(),subList);
//            }
//        }
//        List<TargetTree> targetTrees = map.get("0");
//        for (TargetTree targetTree : targetTrees) {
//            appendChildren(targetTree,map);
//        }
//        return targetTrees;
//
//
//    }
//
//    private void appendChildren(TargetTree targetTree, HashMap<String, List<TargetTree>> map) {
//        Integer targetTreeId = targetTree.getId();
//        if (map.containsKey(targetTreeId.toString())){
//            List<TargetTree> targetTrees = map.get(targetTreeId.toString());
//            targetTree.setChildrenTarget(targetTrees);
//            for (TargetTree tree : targetTrees) {
//                appendChildren(tree,map);
//            }
//        }
//    }
//}
